package cz.muni.fi.pa165.musiclibrary.musiclibraryweb.components;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import cz.muni.fi.pa165.musiclibrary.api.service.auth.AuthException;
import cz.muni.fi.pa165.musiclibrary.api.service.auth.IAuthService;
import cz.muni.fi.pa165.musiclibrary.api.service.auth.IUser;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.MusicLibrarySession;
import cz.muni.fi.pa165.musiclibrary.musiclibraryweb.pages.search.SearchPage;

/**
 *
 * @author Martin Szabo 2013-01-25T20:43:23+0100
 */
public final class LoginPanel extends Panel {

    @SpringBean
    private IAuthService authService;

    public LoginPanel(String id) {
        super(id);
    }

    @Override
    protected void onInitialize() {
        super.onInitialize();
        this.initComponents();
    }

    private void initComponents() {
        final FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        feedbackPanel.setOutputMarkupId(true);
        this.add(feedbackPanel);

        final Form<Void> loginForm = new Form<Void>(ComponentIDs.LOGIN_FORM);
        this.add(loginForm);

        final RequiredTextField<String> nameTextField = new RequiredTextField<String>(ComponentIDs.USER_NAME, new Model<String>());
        nameTextField.setOutputMarkupId(true);
        loginForm.add(nameTextField);
        final PasswordTextField passTextField = new PasswordTextField(ComponentIDs.USER_PASSWORD, new Model<String>());
        passTextField.setOutputMarkupId(true);
        loginForm.add(passTextField);

        final MusicLibrarySession session = MusicLibrarySession.get();
        String userName = session.isAuthorized() ? session.getUser().getUserName() : "";
        final Label logoutLabel = new Label(ComponentIDs.LOGED_USER_LABEL, userName);

        final Link<Void> logoutButton = new Link<Void>(ComponentIDs.LOGOUT_BUTTON) {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                IUser user = session.getUser();
                String userName = user != null ? user.getUserName() : null;
                authService.logout(userName);
                session.setUser(null);
                this.setResponsePage(SearchPage.class);
            }
        };
        this.add(logoutButton);

        loginForm.add(new AjaxSubmitLinkWithErrorClass(ComponentIDs.LOGIN_BUTTON, loginForm) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                try {
                    IUser user = authService.login(nameTextField.getValue(), passTextField.getValue());
                    session.setUser(user);
                    this.setResponsePage(SearchPage.class);
                } catch (AuthException ex) {
                    String errorMessage = new StringResourceModel("LoginPanel.form.auth.error", this, null).getObject();
                    target.appendJavaScript(String.format("alert('%1$s');", errorMessage));
                    super.onSubmit();
                }
            }
        });

        logoutButton.setVisible(session.isAuthorized());
        logoutLabel.setVisible(session.isAuthorized());
        loginForm.setVisible(!session.isAuthorized());
        this.add(logoutLabel, loginForm);
    }

    private static final class ComponentIDs {

        private static final String USER_NAME = "name";
        private static final String USER_PASSWORD = "pass";
        private static final String LOGIN_BUTTON = "login";
        private static final String LOGOUT_BUTTON = "logout";
        private static final String LOGIN_FORM = "loginForm";
        private static final String LOGED_USER_LABEL = "logedUserLable";
    }
}
